package edu.ccut.saturn.studio.dialog.validatedialog;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;
import org.w3c.dom.Document;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher.SearchResult;
import edu.ccut.saturn.studio.core.search.dialog.OneCompontentSearchDialog;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnDialogUtil;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnSWTUtils;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnValidation;
import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnArg;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnValidate;
import edu.ccut.saturn.studio.naviagtor.SaturnFile;
import edu.ccut.saturn.studio.saturndata.model.ValidateModel;
import edu.ccut.saturn.studio.saturndata.model.ValidateVO;

/**
 * TODO:把大量button改成checkbox
 */
public class ValidateDialog extends TitleAreaDialog {

	public ValidateDialog(Shell parentShell) {
		super(parentShell);
	}

	private Label keyLabel, imageLabel, nameLabel, JSValidateLabel,
			userDefineJSLabel, validateErrorInformationLabel, descriptionLabel,
			eventTypeLabel, validateTypeLabel, argListLabel;

	private Text keyText, nameText, JSValidateText, userDefineJSText,
			descriptiontext;

	private Button onblurButton, onchangeButton, onclickButton,
			ondbclickButton, onkeydownButton, onkeypressButton, onfocusButton,
			onkeyupButton, onmousedownButton, onmousemoveButton,
			onmouseoutButton, onmouseoverButton, onmouseupButton;

	private Combo validateTypeCombo;

	private TableViewer argListTableViewer, errorInfomationTableViewer;

	private Table table, table2;

	private ValidateVO validateVO = null;

	private SaturnValidate saturnValidate = new SaturnValidate();

	private int temp = 0;

	public static String VALIDATE_FILE_SUFFIX = "validate";

	private List<SearchResult> validateSearchResult = null;

	private Shape shape;

	protected Shell shell;

	/**
	 * Create the dialog
	 * 
	 * @param parentShell
	 */

	public ValidateVO getValidateVO() {
		return this.validateVO;
	}

	public ValidateDialog(Shell parent, ValidateVO validateVO, Shape shape) {
		this(parent);
		this.shape = shape;
		validateVO = this.getValidateVO();
		this.validateVO = validateVO;
	}

	public ValidateVO setValidateVO(ValidateVO validateVO) {
		return this.validateVO = validateVO;
	}

	/**
	 * 用 SWT.RESIZE、SWT.MAX、SWT.MIN 分别设置窗口为可变大小、可最大化、可最小化
	 */
	protected int getShellStyle() {
		return super.getShellStyle() | SWT.MIN | SWT.MAX | SWT.RESIZE;
	}

	/**
	 * Return the initial size of the dialog
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(520, 745);
	}

	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		// 设置标题
		setTitle(Messages.getString("ValidateAddDialog.validatemessage")); //$NON-NLS-1$
		// 设置对话框样式效果
		setMessage(Messages.getString("ValidateAddDialog.validateproperty")); //$NON-NLS-1$
		return contents;
	}

	/**
	 * Create contents of the button bar
	 * 
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, Messages
				.getString("ValidateAddDialog.confirm"), false); //$NON-NLS-1$
		createButton(parent, IDialogConstants.CANCEL_ID, Messages
				.getString("ValidateAddDialog.cancel"), true); //$NON-NLS-1$
		getButton(IDialogConstants.OK_ID).setEnabled(false);
	}

	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			if (("".equals(keyText.getText().trim()))
					&& ("".equals(nameText.getText().trim()))) {
				getShell().dispose();
				return;
			}
			this.validateVO.setValidateType(validateTypeCombo.getText().trim());

			this.validateVO.setKey(keyText.getText().trim());
			this.validateVO.setName(nameText.getText().trim());
			this.validateVO.setJSMethodName(JSValidateText.getText().trim());
			this.validateVO.setCustemJSMethodInfo(userDefineJSText.getText()
					.trim());
			this.validateVO.setJSMethodName(JSValidateText.getText().trim());

			List<String> eventInfoList = new ArrayList<String>();

			if (onblurButton.getSelection() == true) {
				eventInfoList.add(onblurButton.getText().trim());
			}
			if (onchangeButton.getSelection() == true) {
				eventInfoList.add(onchangeButton.getText().trim());
			}
			if (onclickButton.getSelection() == true) {
				eventInfoList.add(onclickButton.getText().trim());
			}
			if (ondbclickButton.getSelection() == true) {
				eventInfoList.add(ondbclickButton.getText().trim());
			}
			if (onkeydownButton.getSelection() == true) {
				eventInfoList.add(onkeydownButton.getText().trim());
			}
			if (onkeypressButton.getSelection() == true) {
				eventInfoList.add(onkeypressButton.getText().trim());
			}
			if (onfocusButton.getSelection() == true) {
				eventInfoList.add(onfocusButton.getText().trim());
			}
			if (onkeyupButton.getSelection() == true) {
				eventInfoList.add(onkeyupButton.getText().trim());
			}
			if (onmousedownButton.getSelection() == true) {
				eventInfoList.add(onmousedownButton.getText().trim());
			}
			if (onmousemoveButton.getSelection() == true) {
				eventInfoList.add(onmousemoveButton.getText().trim());
			}
			if (onmouseoutButton.getSelection() == true) {
				eventInfoList.add(onmouseoutButton.getText().trim());
			}
			if (onmouseoverButton.getSelection() == true) {
				eventInfoList.add(onmouseoverButton.getText().trim());
			}
			if (onmouseupButton.getSelection() == true) {
				eventInfoList.add(onmouseupButton.getText().trim());
			}

			this.validateVO.setEventInfos(eventInfoList);
		} else {
			this.setValidateVO(null);
		}
		super.buttonPressed(buttonId);
	}

	/**
	 * Create contents of the dialog
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		if (this.validateVO == null) {
			getShell()
					.setText(
							Messages
									.getString("ValidateAddDialog.addvalidatemessageinterface")); //$NON-NLS-1$
		} else {
			getShell()
					.setText(
							Messages
									.getString("ValidateAddDialog.altervalidatemessageinterface")); //$NON-NLS-1$
		}

		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);

		final GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, true,
				true);
		gd_composite.widthHint = 520;
		container.setLayoutData(gd_composite);
		final GridLayout gridLayout = new GridLayout(3, false);
		gridLayout.marginRight = 3;
		gridLayout.marginLeft = 3;
		gridLayout.marginTop = 5;
		container.setLayout(gridLayout);

		keyLabel = new Label(container, SWT.NONE);
		GridData keyLabelGridData = new GridData();
		keyLabel.setLayoutData(keyLabelGridData);
		keyLabel.setText(Messages.getString("ValidateAddDialog.validateid")); //$NON-NLS-1$

		keyText = new Text(container, SWT.BORDER | SWT.READ_ONLY);
		keyText.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				getButton(IDialogConstants.OK_ID).setEnabled(true);
				setErrorMessage(null);
			}

		});
		GridData gd_keyText = new GridData(SWT.FILL, SWT.FILL, true, false);
		keyText.setLayoutData(gd_keyText);

		imageLabel = new Label(container, SWT.BORDER);
		imageLabel.setImage(Activator.getImage(Images.AMPLIFY_MINUS));
		imageLabel.addMouseTrackListener(new MouseTrackAdapter() {
			public void mouseEnter(final MouseEvent e) {
				imageLabel.setImage(Activator.getImage(Images.AMPLIFY_MINUS));
			}

			public void mouseExit(final MouseEvent e) {
				imageLabel.setImage(Activator.getImage(Images.AMPLIFY_PLUS));
			}
		});
		imageLabel.setImage(Activator.getImage(Images.AMPLIFY_PLUS));
		imageLabel.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {

				final ElementListSelectionDialog validateKeyDialog = SaturnDialogUtil
						.createElementListSelectionDialog(getShell(),
								new SaturnSWTUtils.SaturnLabelDefaultProvider() {
									public Image getImage(Object element) {
										return SaturnFile
												.getImageFromFileType(VALIDATE_FILE_SUFFIX);
									}// 添加小图片信息

									@Override
									public String getText(Object element) {
										return element.toString();
									}
								});

				validateKeyDialog.setTitle(Messages
						.getString("ValidateAddDialog.propertyconstitute")); //$NON-NLS-1$

				try {
					SaturnDialogUtil.createProgressMonitorDialog(getShell())
							.run(true, true, new IRunnableWithProgress() {
								public void run(IProgressMonitor monitor)
										throws InvocationTargetException {
									try {
										String[] ss = { "validate" };
										validateSearchResult = SaturnFileSearcher
												.getValueAndDocumentMapInWorkspace(
														ss, null, monitor);

										List<String> results = new ArrayList<String>();

										for (SearchResult result : validateSearchResult) {

											validateVO = getvalidate(result
													.getDocument());
											results.add(validateVO.getName());
											setValidateVO(validateVO);
										}
										Object[] keys = results.toArray();
										validateKeyDialog.setElements(keys);
									} catch (Exception e) {
										throw new InvocationTargetException(e);
									} finally {
										monitor.done();
									}
								}
							});
				} catch (Exception ee) {
					ee.printStackTrace();
				}

				if (validateKeyDialog.open() == Window.OK
						&& validateSearchResult != null) {

					String key = (String) validateKeyDialog.getFirstResult();

					for (SearchResult result : validateSearchResult) {

						ValidateVO validateInfo = getvalidate(result
								.getDocument());

						if (validateInfo.getName().equals(key)) {
							String validateid = result.getKey()
									+ validateInfo.getKey();
							validateInfo.setKey(validateid);
							setValidateVO(validateInfo);
							break;

						}
					}
					inialComponentByValidate();
				}
			}
		});

		nameLabel = new Label(container, SWT.NONE);
		nameLabel.setText(Messages.getString("ValidateAddDialog.validatename")); //$NON-NLS-1$

		nameText = new Text(container, SWT.BORDER | SWT.READ_ONLY);
		GridData gd_nameText = new GridData(SWT.FILL, SWT.FILL, true, false);
		gd_nameText.horizontalSpan = 2;
		nameText.setLayoutData(gd_nameText);

		JSValidateLabel = new Label(container, SWT.NONE);
		JSValidateLabel.setText(Messages
				.getString("ValidateAddDialog.jsvalidate")); //$NON-NLS-1$

		JSValidateText = new Text(container, SWT.BORDER | SWT.READ_ONLY);
		GridData gd_jSValidateText = new GridData(SWT.FILL, SWT.CENTER, true,
				false);
		gd_jSValidateText.horizontalSpan = 2;
		JSValidateText.setLayoutData(gd_jSValidateText);

		userDefineJSLabel = new Label(container, SWT.NONE | SWT.WRAP);
		userDefineJSLabel.setText(Messages
				.getString("ValidateAddDialog.userdefinedjs")); //$NON-NLS-1$

		userDefineJSText = new Text(container, SWT.BORDER | SWT.READ_ONLY);
		GridData gd_userDefineJSText = new GridData(SWT.FILL, SWT.FILL, true,
				false);
		gd_userDefineJSText.horizontalSpan = 2;
		userDefineJSText.setLayoutData(gd_userDefineJSText);

		validateErrorInformationLabel = new Label(container, SWT.NONE);
		final GridData gd_validateErrorInformationLabel = new GridData(
				SWT.LEFT, SWT.TOP, false, true);
		gd_validateErrorInformationLabel.verticalIndent = 5;
		validateErrorInformationLabel
				.setLayoutData(gd_validateErrorInformationLabel);
		validateErrorInformationLabel.setText(Messages
				.getString("ValidateAddDialog.validateerrormessage")); //$NON-NLS-1$

		errorInfomationTableViewer = new TableViewer(container, SWT.MULTI
				| SWT.BORDER | SWT.FULL_SELECTION);
		table = errorInfomationTableViewer.getTable();

		GridData gridData1 = new GridData(SWT.FILL, SWT.FILL, false, true, 2, 1);
		gridData1.widthHint = 340;
		gridData1.heightHint = 25;
		table.setLayoutData(gridData1);
		table.setHeaderVisible(true); // 显示表头
		table.setLinesVisible(true); // 显示表格线

		final TableColumn newColumnTableColumn_key = new TableColumn(table,
				SWT.NONE);
		newColumnTableColumn_key.setWidth(120);
		newColumnTableColumn_key.setText("key"); //$NON-NLS-1$

		final TableColumn newColumnTableColumn_value = new TableColumn(table,
				SWT.NONE);
		newColumnTableColumn_value.setWidth(200);
		newColumnTableColumn_value.setText("value"); //$NON-NLS-1$

		errorInfomationTableViewer
				.setContentProvider(new SaturnSWTUtils.SaturnTableViewerDefaultContentProvider() {

					@SuppressWarnings("unchecked")//$NON-NLS-1$
					@Override
					public Object[] getElements(Object inputElement) {
						Map<String, String> errorInfo = (Map<String, String>) inputElement;

						Object[] input = new String[errorInfo.size()];

						int i = 0;
						for (String key : errorInfo.keySet()) {
							input[i++] = key + "::" + errorInfo.get(key); //$NON-NLS-1$
						}

						return input;
					}

				});
		errorInfomationTableViewer
				.setLabelProvider(new SaturnSWTUtils.SaturnTableViewerDefaultLabelProvider() {

					@Override
					public String getColumnText(Object obj, int index) {
						String[] errorInfos = obj.toString().split("::"); //$NON-NLS-1$

						return errorInfos[index];
					}
				});

		descriptionLabel = new Label(container, SWT.NONE);
		final GridData gd_descriptionLabel = new GridData(SWT.FILL, SWT.TOP,
				false, false);
		gd_descriptionLabel.verticalIndent = 5;
		descriptionLabel.setLayoutData(gd_descriptionLabel);
		descriptionLabel.setText(Messages
				.getString("ValidateAddDialog.descriptionmessage")); //$NON-NLS-1$

		descriptiontext = new Text(container, SWT.V_SCROLL | SWT.MULTI
				| SWT.BORDER);
		descriptiontext.setTextLimit(200);
		GridData gridDatadescription = new GridData(SWT.FILL, SWT.FILL, true,
				true, 2, 1);
		gridDatadescription.heightHint = 20;
		descriptiontext.setLayoutData(gridDatadescription);

		eventTypeLabel = new Label(container, SWT.NONE);
		final GridData gd_eventTypeLabel = new GridData(SWT.LEFT, SWT.TOP,
				false, false);
		gd_eventTypeLabel.verticalIndent = 5;
		eventTypeLabel.setLayoutData(gd_eventTypeLabel);
		eventTypeLabel.setText(Messages
				.getString("ValidateAddDialog.eventtype")); //$NON-NLS-1$

		Composite littleComposite = new Composite(container, SWT.BORDER);
		littleComposite.setLayout(new FormLayout());
		GridData gd_eventTypeText = new GridData(SWT.FILL, SWT.FILL, true,
				true, 2, 1);
		gd_eventTypeText.heightHint = 60;
		littleComposite.setLayoutData(gd_eventTypeText);

		onblurButton = new Button(littleComposite, SWT.CHECK);
		onblurButton.setSelection(true);
		final FormData fd_onblurButton = new FormData();
		fd_onblurButton.top = new FormAttachment(0, 15);
		fd_onblurButton.left = new FormAttachment(0, 20);
		onblurButton.setLayoutData(fd_onblurButton);
		onblurButton.setText("onblur"); //$NON-NLS-1$

		onchangeButton = new Button(littleComposite, SWT.CHECK);
		final FormData fd_onchangeButton = new FormData();
		fd_onchangeButton.top = new FormAttachment(0, 45);
		fd_onchangeButton.left = new FormAttachment(onblurButton, 0, SWT.LEFT);
		onchangeButton.setLayoutData(fd_onchangeButton);
		onchangeButton.setText("onchange"); //$NON-NLS-1$

		onclickButton = new Button(littleComposite, SWT.CHECK);
		final FormData fd_onclickButton = new FormData();
		fd_onclickButton.top = new FormAttachment(0, 75);
		fd_onclickButton.left = new FormAttachment(onchangeButton, 0, SWT.LEFT);
		onclickButton.setLayoutData(fd_onclickButton);
		onclickButton.setText("onclick"); //$NON-NLS-1$

		ondbclickButton = new Button(littleComposite, SWT.CHECK);
		final FormData fd_ondbclickButton = new FormData();
		fd_ondbclickButton.top = new FormAttachment(0, 105);
		fd_ondbclickButton.left = new FormAttachment(onclickButton, 0, SWT.LEFT);
		ondbclickButton.setLayoutData(fd_ondbclickButton);
		ondbclickButton.setText("ondbclick"); //$NON-NLS-1$

		onkeydownButton = new Button(littleComposite, SWT.CHECK);
		final FormData fd_onkeydownButton = new FormData();
		fd_onkeydownButton.top = new FormAttachment(onblurButton, 0, SWT.TOP);
		fd_onkeydownButton.left = new FormAttachment(onchangeButton, 5,
				SWT.RIGHT);
		onkeydownButton.setLayoutData(fd_onkeydownButton);
		onkeydownButton.setText("onkeydown"); //$NON-NLS-1$

		onkeypressButton = new Button(littleComposite, SWT.CHECK);
		final FormData fd_onkeypressButton = new FormData();
		fd_onkeypressButton.top = new FormAttachment(onchangeButton, 0, SWT.TOP);
		fd_onkeypressButton.left = new FormAttachment(onkeydownButton, 0,
				SWT.LEFT);
		onkeypressButton.setLayoutData(fd_onkeypressButton);
		onkeypressButton.setText("onkeypress"); //$NON-NLS-1$

		onfocusButton = new Button(littleComposite, SWT.CHECK);
		final FormData fd_onfocusButton = new FormData();
		fd_onfocusButton.top = new FormAttachment(onclickButton, 0, SWT.TOP);
		fd_onfocusButton.left = new FormAttachment(onkeypressButton, 0,
				SWT.LEFT);
		onfocusButton.setLayoutData(fd_onfocusButton);
		onfocusButton.setText("onfocus"); //$NON-NLS-1$

		onkeyupButton = new Button(littleComposite, SWT.CHECK);
		final FormData fd_onkeyupButton = new FormData();
		fd_onkeyupButton.top = new FormAttachment(ondbclickButton, 0, SWT.TOP);
		fd_onkeyupButton.left = new FormAttachment(onfocusButton, 0, SWT.LEFT);
		onkeyupButton.setLayoutData(fd_onkeyupButton);
		onkeyupButton.setText("onkeyup"); //$NON-NLS-1$

		onmousedownButton = new Button(littleComposite, SWT.CHECK);
		final FormData fd_onmousedownButton = new FormData();
		fd_onmousedownButton.top = new FormAttachment(onkeydownButton, 0,
				SWT.TOP);
		fd_onmousedownButton.left = new FormAttachment(0, 180);
		onmousedownButton.setLayoutData(fd_onmousedownButton);
		onmousedownButton.setText("onmousedown"); //$NON-NLS-1$

		onmousemoveButton = new Button(littleComposite, SWT.CHECK);
		final FormData fd_onmousemoveButton = new FormData();
		fd_onmousemoveButton.top = new FormAttachment(onkeypressButton, 0,
				SWT.TOP);
		fd_onmousemoveButton.left = new FormAttachment(onmousedownButton, 0,
				SWT.LEFT);
		onmousemoveButton.setLayoutData(fd_onmousemoveButton);
		onmousemoveButton.setText("onmousemove"); //$NON-NLS-1$

		onmouseoutButton = new Button(littleComposite, SWT.CHECK);
		final FormData fd_onmouseoutButton = new FormData();
		fd_onmouseoutButton.top = new FormAttachment(onfocusButton, 0, SWT.TOP);
		fd_onmouseoutButton.left = new FormAttachment(onmousemoveButton, 0,
				SWT.LEFT);
		onmouseoutButton.setLayoutData(fd_onmouseoutButton);
		onmouseoutButton.setText("onmouseout"); //$NON-NLS-1$

		onmouseoverButton = new Button(littleComposite, SWT.CHECK);
		final FormData fd_onmouseoverButton = new FormData();
		fd_onmouseoverButton.top = new FormAttachment(onkeyupButton, 0, SWT.TOP);
		fd_onmouseoverButton.left = new FormAttachment(onmouseoutButton, 0,
				SWT.LEFT);
		onmouseoverButton.setLayoutData(fd_onmouseoverButton);
		onmouseoverButton.setText("onmouseover"); //$NON-NLS-1$

		onmouseupButton = new Button(littleComposite, SWT.CHECK);
		final FormData fd_onmouseupButton = new FormData();
		fd_onmouseupButton.top = new FormAttachment(onmousedownButton, 0,
				SWT.TOP);
		fd_onmouseupButton.left = new FormAttachment(0, 290);
		onmouseupButton.setLayoutData(fd_onmouseupButton);
		onmouseupButton.setText("onmouseup"); //$NON-NLS-1$

		validateTypeLabel = new Label(container, SWT.NONE);
		final GridData gd_validateTypeLabel = new GridData(SWT.LEFT, SWT.TOP,
				false, false);
		validateTypeLabel.setLayoutData(gd_validateTypeLabel);
		validateTypeLabel.setText(Messages
				.getString("ValidateAddDialog.validatetype")); //$NON-NLS-1$

		validateTypeCombo = new Combo(container, SWT.BORDER | SWT.READ_ONLY);
		GridData gd_validateTypeCombo = new GridData(SWT.FILL, SWT.FILL, true,
				false, 2, 1);
		validateTypeCombo.setLayoutData(gd_validateTypeCombo);
		validateTypeCombo
				.setItems(new String[] {
						Messages.getString("ValidateAddDialog.warn"), Messages.getString("ValidateAddDialog.error") }); //$NON-NLS-1$ //$NON-NLS-2$
		validateTypeCombo.select(0);

		argListLabel = new Label(container, SWT.NONE);
		GridData gd_argListLabel = new GridData(SWT.FILL, SWT.TOP, false, false);
		gd_argListLabel.verticalIndent = 5;
		argListLabel.setLayoutData(gd_argListLabel);
		argListLabel.setText(Messages
				.getString("ValidateAddDialog.parameterlist")); //$NON-NLS-1$

		argListTableViewer = new TableViewer(container, SWT.MULTI | SWT.BORDER
				| SWT.FULL_SELECTION);
		argListTableViewer
				.setContentProvider(new SaturnSWTUtils.SaturnTableViewerDefaultContentProvider());
		argListTableViewer
				.setLabelProvider(new SaturnSWTUtils.SaturnTableViewerDefaultLabelProvider() {

					@Override
					public String getColumnText(Object obj, int index) {
						return obj.toString();
					}
				});

		table2 = argListTableViewer.getTable();

		GridData gridData2 = new GridData(SWT.FILL, SWT.FILL, true, true);
		gridData2.widthHint = 340;
		gridData2.heightHint = 25;
		table2.setLayoutData(gridData2);
		table2.setHeaderVisible(true); // 显示表头
		table2.setLinesVisible(true); // 显示表格线

		final TableColumn newColumnTableColumn1 = new TableColumn(table2,
				SWT.NONE);
		newColumnTableColumn1.setWidth(200);
		newColumnTableColumn1.setText(Messages
				.getString("ValidateAddDialog.expression")); //$NON-NLS-1$

		this.modifyParameter();

		if (SaturnValidation.isEmpty(keyText.getText().trim())) {
			this.setErrorMessage(Messages.getString("ValidateDialog.ChooseID")); //$NON-NLS-1$
		}

		return area;
	}

	private ValidateVO getvalidate(Document document) {
		List<ValidateVO> validateInfos = ValidateModel.getInstance()
				.loadValidates(document.getDocumentElement());
		return validateInfos.get(0);
	}

	private void inialComponentByValidate() {
		if (this.validateVO != null) {
			keyText.setText(validateVO.getKey());
			nameText.setText(validateVO.getName());
			JSValidateText.setText(validateVO.getJSMethodName());
			userDefineJSText.setText(validateVO.getCustemJSMethodInfo());
			descriptiontext.setText(validateVO.getDescription());
			validateTypeCombo.setText(validateVO.getValidateType());

			errorInfomationTableViewer.setInput(validateVO.getErrorInfos());
			argListTableViewer.setInput(validateVO.getArgs());

			this.onblurButton.setSelection(false);
			this.onchangeButton.setSelection(false);
			this.onclickButton.setSelection(false);
			this.ondbclickButton.setSelection(false);
			this.onkeydownButton.setSelection(false);
			this.onkeypressButton.setSelection(false);
			this.onfocusButton.setSelection(false);
			this.onkeyupButton.setSelection(false);
			this.onmousedownButton.setSelection(false);
			this.onmouseoutButton.setSelection(false);
			this.onmouseoverButton.setSelection(false);
			this.onmouseupButton.setSelection(false);

			List<String> eventInfoList = validateVO.getEventInfos();
			if ((eventInfoList != null) && !eventInfoList.isEmpty()) {
				for (int i = 0; i < eventInfoList.size(); i++) {
					if (eventInfoList.get(i).equals(
							this.onblurButton.getText().trim())) {
						this.onblurButton.setSelection(true);
					}
					if (eventInfoList.get(i).equals(
							this.onchangeButton.getText().trim())) {
						this.onchangeButton.setSelection(true);
					}
					if (eventInfoList.get(i).equals(
							this.onclickButton.getText().trim())) {
						this.onclickButton.setSelection(true);
					}
					if (eventInfoList.get(i).equals(
							this.ondbclickButton.getText().trim())) {
						this.ondbclickButton.setSelection(true);
					}
					if (eventInfoList.get(i).equals(
							this.onkeydownButton.getText().trim())) {
						this.onkeydownButton.setSelection(true);
					}
					if (eventInfoList.get(i).equals(
							this.onkeypressButton.getText().trim())) {
						this.onkeypressButton.setSelection(true);
					}
					if (eventInfoList.get(i).equals(
							this.onfocusButton.getText().trim())) {
						this.onfocusButton.setSelection(true);
					}
					if (eventInfoList.get(i).equals(
							this.onkeyupButton.getText().trim())) {
						this.onkeyupButton.setSelection(true);
					}
					if (eventInfoList.get(i).equals(
							this.onmousedownButton.getText().trim())) {
						this.onmousedownButton.setSelection(true);
					}
					if (eventInfoList.get(i).equals(
							this.onmousemoveButton.getText().trim())) {
						this.onmousemoveButton.setSelection(true);
					}
					if (eventInfoList.get(i).equals(
							this.onmouseoutButton.getText().trim())) {
						this.onmouseoutButton.setSelection(true);
					}
					if (eventInfoList.get(i).equals(
							this.onmouseoverButton.getText().trim())) {
						this.onmouseoverButton.setSelection(true);
					}
					if (eventInfoList.get(i).equals(
							this.onmouseupButton.getText().trim())) {
						this.onmouseupButton.setSelection(true);
					}
				}
			}
		}
	}

	protected void modifyParameter() {
		// 建立一个表格单元编辑器
		CellEditor[] editors = new CellEditor[1];
		editors[0] = new TextCellEditor(table2);
		// 设置列属性
		argListTableViewer.setColumnProperties(new String[] { "arg" }); //$NON-NLS-1$
		// 设置表格单元的编辑器
		argListTableViewer.setCellEditors(editors);
		// 设置表格单元的修改器
		argListTableViewer.setCellModifier(new ICellModifier() {

			public boolean canModify(Object element, String property) {
				return true;
			}

			public Object getValue(Object element, String property) {
				if (property.equals("arg")) { //$NON-NLS-1$
					return element.toString();
				}

				return ""; //$NON-NLS-1$
			}

			public void modify(Object element, String property, Object value) {
				if (property.equals("arg")) { //$NON-NLS-1$
					List<String> args = validateVO.getArgs();

					TableItem tableItem = (TableItem) element;
					int index = args.indexOf(tableItem.getData());

					if (index >= 0) {
						args.remove(index);
						args.add(index, value.toString());
					}

					tableItem.setData(value);
				}

				argListTableViewer.refresh();
			}
		});
	}

	public int getTemp() {
		return temp;
	}

	public void setTemp(int temp) {
		this.temp = temp;
	}

	public SaturnValidate getSaturnValidate() {
		return saturnValidate;
	}

	public void setSaturnValidate(SaturnValidate saturnValidate) {
		this.saturnValidate = saturnValidate;
	}

}
